﻿using CPPEI.Coolzon.UI.XProperty.Editors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.DataPartLib.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for DataRangeEditWindow.xaml
    /// </summary>
    public partial class DataRangeEditWindow : Window
    {
        public List<DataRange> RetDataRangeList { get; private set; }
        private System.Collections.ObjectModel.ObservableCollection<DataRange> DataRangeList;
        public DataRangeEditWindow(List<DataRange> dataRangeList)
        {
            InitializeComponent();
            DataRangeList = new System.Collections.ObjectModel.ObservableCollection<DataRange>();
            if (dataRangeList != null)
            {
                foreach (var item in dataRangeList)
                {
                    DataRangeList.Add(item);
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _ctlDataRangeList.ItemsSource = DataRangeList;
        }

        private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            StackPanel sp = (StackPanel)VisualUpwardSearch<StackPanel>(button);
            // 显示brush编辑窗口
            BrushEditWindow window = new BrushEditWindow(sp.Tag as BrushEx);
            if (window.ShowDialog() == true)
            {
                sp.Tag = window.ReturnBrush;
                sp.Background = window.ReturnBrush.Brush;
            }
        }
        static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            DataRange newDataRange = new DataRange();
            newDataRange.StartValue = 0;
            newDataRange.EndValue = 25;
            newDataRange.StartWidth = 0.12;
            newDataRange.EndWidth = 0.12;
            DataRangeList.Add(newDataRange);
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if(_ctlDataRangeList.SelectedItem != null)
            {
                DataRangeList.Remove(_ctlDataRangeList.SelectedItem as DataRange);
            }
        }

        private void _ctlUp_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlDataRangeList.SelectedItem != null)
            {
                DataRange tempDataRange = _ctlDataRangeList.SelectedItem as DataRange;
                int index = _ctlDataRangeList.Items.IndexOf(tempDataRange);
                if (index > 0)
                {
                    DataRangeList.RemoveAt(index);
                    DataRangeList.Insert(index - 1, tempDataRange);
                    _ctlDataRangeList.Items.MoveCurrentTo(tempDataRange);
                }
            }
        }

        private void _ctlDown_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlDataRangeList.SelectedItem != null)
            {
                DataRange tempDataRange = _ctlDataRangeList.SelectedItem as DataRange;
                int index = _ctlDataRangeList.Items.IndexOf(tempDataRange);
                if (index < DataRangeList.Count - 1)
                {
                    DataRangeList.RemoveAt(index);
                    DataRangeList.Insert(index + 1, tempDataRange);
                    _ctlDataRangeList.Items.MoveCurrentTo(tempDataRange);
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RetDataRangeList = new List<DataRange>();
            foreach(var item in DataRangeList)
            {
                RetDataRangeList.Add(item);
            }
            DialogResult = true;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void _ctlDataRangeList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (_ctlDataRangeList.SelectedItem != null)
            {
                _ctlDataRangeDetail.IsEnabled = true;

                Binding binding = new Binding();
                binding.Source = _ctlDataRangeList.SelectedItem;
                _ctlDataRangeDetail.SetBinding(Grid.DataContextProperty, binding);
            }
            else
            {
                _ctlDataRangeDetail.IsEnabled = false;
                BindingOperations.ClearBinding(_ctlDataRangeDetail, Grid.DataContextProperty);
            }
        }
    }
}
